package example.exam.Shopee;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 假设点P处于坐标轴原点，W表示向上，A表示向左，S表示向下，D表示向右，
 * 输入一串指令代表P点的移动轨迹，输出最终P的位置
 * <p>
 * 例如 "2W2D"表示向上移动两个坐标位置，再向右移动两个坐标位置
 * <p>
 * "W2D"表示向上移动一个坐标位置，再向右移动两个坐标位置
 */
public class Demo3 {

    /**
     * 不考虑符号位
     *
     * @param order
     * @return
     */
    public static int[] GetEndPoint(String order) {
        char[] chars = order.toCharArray();
        int x = 0;
        int y = 0;
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (char aChar : chars) {
            if (aChar >= '0' && aChar <= '9') {
                linkedList.addFirst(Character.getNumericValue(aChar));
            } else {
//                if (!(aChar == 'W' || aChar == 'w' || aChar == 'D' || aChar == 'd' || aChar == 'A' || aChar == 'a' || aChar == 'S' || aChar == 's')) {
//                    continue;
//                }
                int sum = 0;
                //20原先  --02队列  -- 出队20
                while (linkedList.size() != 0) {
                    sum = sum * 10 + linkedList.removeLast();
                }
                if (aChar == 'W' || aChar == 'w') {//上
                    if (sum == 0) {
                        x++;
                    } else {
                        x += sum;
                    }
                } else if (aChar == 'D' || aChar == 'd') {//右
                    if (sum == 0) {
                        y++;
                    } else {
                        y += sum;
                    }
                } else if (aChar == 'A' || aChar == 'a') {//左
                    if (sum == 0) {
                        x--;
                    } else {
                        x -= sum;
                    }
                } else if (aChar == 'S' || aChar == 's') {//下
                    if (sum == 0) {
                        y--;
                    } else {
                        y -= sum;
                    }
                }
            }
        }
        return new int[]{x, y};
    }
    /**
     * 考虑了符号位
     *
     * @param order
     * @return
     */
    public static int[] GetEndPoint2(String order) {
        char[] chars = order.toCharArray();
        int x = 0;
        int y = 0;
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (char aChar : chars) {
            // -20  02-
            if ((aChar >= '0' && aChar <= '9') || aChar == '-') {
                linkedList.addFirst(Character.getNumericValue(aChar));
            } else {
                int sum = 0;
                //20原先  --02队列  -- 出队20
                boolean flag = false;// false + ；true -
                while (linkedList.size() != 0) {
                    //-是在最后面的
                    if (linkedList.get(linkedList.size() - 1) == Character.getNumericValue('-')) {
                        flag = true;
                        linkedList.removeLast();
                        //只有一个符号位
                        if (linkedList.size() == 0) {
                            break;
                        }
                    }
                    sum = sum * 10 + linkedList.removeLast();
                }
                if (flag) {
                    sum = -sum;
                }
                if (aChar == 'W' || aChar == 'w') {//上
                    if (sum == 0) {
                        if (flag) {
                            x--;
                        } else {
                            x++;
                        }
                    } else {
                        x += sum;
                    }
                } else if (aChar == 'D' || aChar == 'd') {//右
                    if (sum == 0) {
                        if (flag) {
                            y--;
                        } else {
                            y++;
                        }
                    } else {
                        y += sum;
                    }
                } else if (aChar == 'A' || aChar == 'a') {//左
                    if (sum == 0) {
                        if (flag) {
                            x++;
                        } else {
                            x--;
                        }

                    } else {
                        x -= sum;
                    }
                } else if (aChar == 'S' || aChar == 's') {//下
                    if (sum == 0) {
                        if (flag) {
                            y++;
                        } else {
                            y--;
                        }
                    } else {
                        y -= sum;
                    }
                }
            }
        }
        return new int[]{x, y};
    }

    public static void main(String[] args) {
        // 2W2D
//        System.out.println(Arrays.toString(GetEndPoint("2W2D2S2A1a1w1d1s")));
//        System.out.println(Arrays.toString(GetEndPoint("-1W-1DDDA")));
//        System.out.println(Arrays.toString(GetEndPoint("-1W1D")));
//        System.out.println(Arrays.toString(GetEndPoint("-20W")));
//        System.out.println(Arrays.toString(GetEndPoint("-2W-2D")));
    }
}
